package com.ruben.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import com.ruben.listener.RedisMessageListener;

/**
 * @author <achao1441470436@gmail.com>
 * @since 2022/5/8 17:31
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisMessageListener listener, RedisConnectionFactory factory, Jackson2JsonRedisSerializer<Object> serializer) {
        final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        final MessageListenerAdapter adapter = new MessageListenerAdapter(listener, "onMessage");
        adapter.setSerializer(serializer);
        container.addMessageListener(adapter, new ChannelTopic("topic"));
        return container;
    }

}
